package com.hxx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hxx.domain.Order;
import com.hxx.domain.OrderStatus;
import com.hxx.dto.OrderRequest;
import com.hxx.service.DishService;
import com.hxx.service.OrderService;
import com.hxx.service.ai.OrderAiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/orders")
@CrossOrigin
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderAiService orderAiService;

    @Autowired
    private DishService dishService;

    /**
     * 创建订单
     * @param orderRequest
     * @return
     */
    @PostMapping
    public ResponseEntity<Order> createOrder(@RequestBody OrderRequest orderRequest) {
        Order order = orderService.createOrder(orderRequest);
        return ResponseEntity.ok(order);
    }

    /**
     * 从自然语言创建订单
     * @param naturalLanguageInput
     * @return
     */
    @PostMapping("/natural-language")
    public ResponseEntity<Order> createOrderFromNaturalLanguage(@RequestBody String naturalLanguageInput) {
        OrderRequest orderRequest = orderAiService.parseNaturalLanguageOrder(naturalLanguageInput);
        Order order = orderService.createOrder(orderRequest);
        return ResponseEntity.ok(order);
    }

    /**
     * 获取订单
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<Order> getOrder(@PathVariable int id) {
        return ResponseEntity.ok(orderService.getById(id));
    }

    /**
     * 获取所有订单
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<Order>> getAllOrders() {
        return ResponseEntity.ok(orderService.list());
    }

    /**
     * 获取某个顾客的订单
     * @param phone
     * @return
     */
    @GetMapping("/customer/{phone}")
    public ResponseEntity<List<Order>> getOrdersByPhone(@PathVariable String phone) {
        return ResponseEntity.ok(orderService.list(new LambdaQueryWrapper<Order>()
                .eq(Order::getCustomerPhone, phone)));
    }

    /**
     * 更新订单状态
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/{id}/status")
    public ResponseEntity<Order> updateOrderStatus(
            @PathVariable Long id,
            @RequestParam OrderStatus status) {
        Order order = orderService.getById(id);
        //order.setStatus(status);
        // 更新订单
        orderService.updateById(order);
        return ResponseEntity.ok(order);
    }
}
